package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/11 13:46
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //根据用户名查询;
    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    //分页条件查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> users = userDao.findPage(queryString);
        long total = users.getTotal();
        List<User> rows = users.getResult();
        return new PageResult(total, rows);
    }

    //添加数据;
    @Override
    public void add(User user, Integer[] roleIds) {
        //先操作User表;
        String passwordStr = user.getPassword();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode(passwordStr);
        user.setPassword(password);
        userDao.add(user);
        //编辑t_user_role表;
        Integer userId = user.getId();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map map = new HashMap();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.setUserAnRrole(map);
            }
        }
    }

    //删除数据;
    @Override
    public void deleteByid(Integer id) {
        userDao.deleteAssocication(id);
        userDao.deleteByid(id);
    }

    //根据ID查询;
    @Override
    public User findByid(Integer id) {
        return userDao.findByid(id);
    }

    //根据ID 查询关联的 角色;
    @Override
    public List<Integer> getRoleIds(Integer id) {
        return userDao.getRoleIds(id);
    }

    //修改数据;
    @Override
    public void update(User user, Integer[] roleIds) {

        Integer userId = user.getId();
        //编辑user表数据;
        userDao.update(user);
        //清理中间表数据;
        userDao.deleteAssocication(userId);
        //重新添加中间表信息;
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map map = new HashMap();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.setUserAnRrole(map);
            }
        }
    }
}
